<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item label="活动名称">
          <el-input
            v-model="listQuery.title"
            placeholder="请输入活动名称"
            clearable
            @keyup.enter.native="handleFilter"
          />
        </el-form-item>
        <el-form-item label="活动状态">
          <el-select v-model="listQuery.status" placeholder="请选择" clearable>
            <el-option label="进行中" value="进行中" />
            <el-option label="已完成" value="已完成" />
          </el-select>
        </el-form-item>
        <el-form-item label="活动日期">
          <el-date-picker
            v-model="listQuery.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleCreate">新增活动</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
    >
      <el-table-column prop="title" label="活动名称" />
      <el-table-column prop="date" label="活动日期" width="120" />
      <el-table-column prop="location" label="活动地点" width="150" />
      <el-table-column prop="leader" label="负责人" width="100" />
      <el-table-column prop="participants" label="参与人数" width="100" />
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status === '已完成' ? 'success' : 'warning'">
            {{ row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="活动名称" prop="title">
          <el-input v-model="temp.title" />
        </el-form-item>
        <el-form-item label="活动日期" prop="date">
          <el-date-picker
            v-model="temp.date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="活动地点" prop="location">
          <el-input v-model="temp.location" />
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="temp.leader" />
        </el-form-item>
        <el-form-item label="参与人数" prop="participants">
          <el-input-number v-model="temp.participants" :min="1" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="temp.status" placeholder="请选择">
            <el-option label="进行中" value="进行中" />
            <el-option label="已完成" value="已完成" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'

export default {
  name: 'ActivityList',
  components: { Pagination },
  data() {
    return {
      // 模拟的数据库数据
      mockData: [
        {
          id: 1,
          title: '党史学习教育',
          date: '2024-03-15',
          location: '第一会议室',
          leader: '张三',
          participants: 30,
          status: '进行中'
        },
        {
          id: 2,
          title: '主题党日活动',
          date: '2024-03-20',
          location: '学生活动室',
          leader: '李四',
          participants: 25,
          status: '进行中'
        },
        {
          id: 3,
          title: '学生大会',
          date: '2024-02-28',
          location: '大会议室',
          leader: '王五',
          participants: 50,
          status: '已完成'
        }
      ],
      list: [], // 当前显示的列表数据
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        title: undefined,
        status: undefined,
        dateRange: []
      },
      temp: {
        id: undefined,
        title: '',
        date: '',
        location: '',
        leader: '',
        participants: 1,
        status: '进行中'
      },
      dialogVisible: false,
      dialogTitle: '',
      dialogType: '',
      rules: {
        title: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        date: [{ required: true, message: '请选择活动日期', trigger: 'change' }],
        location: [{ required: true, message: '请输入活动地点', trigger: 'blur' }],
        leader: [{ required: true, message: '请输入负责人', trigger: 'blur' }],
        participants: [{ required: true, message: '请输入参与人数', trigger: 'change' }],
        status: [{ required: true, message: '请选择活动状态', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      this.listLoading = true
      setTimeout(() => {
        // 1. 根据查询条件过滤数据
        let result = [...this.mockData]
        
        const { title, status, dateRange } = this.listQuery
        
        if (title) {
          result = result.filter(item => item.title.includes(title))
        }
        
        if (status) {
          result = result.filter(item => item.status === status)
        }
        
        if (dateRange && dateRange.length === 2) {
          const [startDate, endDate] = dateRange
          result = result.filter(item => {
            const activityDate = new Date(item.date)
            return activityDate >= new Date(startDate) && activityDate <= new Date(endDate)
          })
        }

        // 2. 更新总数
        this.total = result.length
        
        // 3. 分页处理
        const { page, limit } = this.listQuery
        const start = (page - 1) * limit
        const end = page * limit
        
        // 4. 设置当前页数据
        this.list = result.slice(start, end)
        
        this.listLoading = false
      }, 500)
    },
    
    // 查询
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    
    // 重置查询
    resetQuery() {
      this.listQuery = {
        page: 1,
        limit: 10,
        title: undefined,
        status: undefined,
        dateRange: []
      }
      this.getList()
    },
    
    // 新增
    handleCreate() {
      this.dialogType = 'create'
      this.dialogTitle = '新增活动'
      this.temp = {
        id: undefined,
        title: '',
        date: '',
        location: '',
        leader: '',
        participants: 1,
        status: '进行中'
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    
    // 编辑
    handleUpdate(row) {
      this.dialogType = 'update'
      this.dialogTitle = '编辑活动'
      this.temp = Object.assign({}, row)
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    
    // 删除
    handleDelete(row) {
      this.$confirm('确认删除该活动吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const index = this.mockData.findIndex(item => item.id === row.id)
        this.mockData.splice(index, 1)
        this.$message.success('删除成功')
        this.getList()
      })
    },
    
    // 保存数据
    saveData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dialogType === 'create') {
            // 新增：生成新的ID并添加到模拟数据中
            const newId = Math.max(...this.mockData.map(item => item.id)) + 1
            this.temp.id = newId
            this.mockData.push(Object.assign({}, this.temp))
          } else {
            // 编辑：更新模拟数据中的对应记录
            const index = this.mockData.findIndex(item => item.id === this.temp.id)
            this.mockData.splice(index, 1, Object.assign({}, this.temp))
          }
          
          this.dialogVisible = false
          this.$message.success(this.dialogType === 'create' ? '新增成功' : '更新成功')
          this.getList()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  padding-bottom: 10px;
  
  .el-form-item {
    margin-bottom: 10px;
  }
}

.dialog-footer {
  text-align: right;
  padding-top: 20px;
}
</style> 